"""
Models collecting shared attributes.
"""

from django.db import models
from base import *

_model_names = ["ObjectType", "Field", "ValidValue",
                "Property", "PropertyGroup"]
__all__ = _model_names

class ObjectType(MetadataObject):
    """Named objects."""
    _collection_sets = ['child_objects']
    parent_object = models.ForeignKey('self',
                                      blank=True,
                                      null=True,
                                      related_name='child_objects')
    name = models.CharField(max_length=256, blank=True)
    description = models.TextField(blank=True)
    multiplicity = models.CharField(max_length=256, choices=CARDINALITY, default='0..1')
    _container_field = 'parent_object'
    def __unicode__(self):
        return self.name

class Field(MetadataObject):
    _collection_sets = []
    name = models.CharField(max_length=256, blank=True)
    description = models.TextField(blank=True)
    datatype = models.CharField(max_length=256, choices=DATA_TYPES, default='string')
    cardinality = models.CharField(max_length=256, choices=CARDINALITY, default='0..1')
    units = models.CharField(max_length=256, blank=True)
    isEnumerated = models.BooleanField(default=False)

    def __unicode__(self):
        return self.name

class ValidValue(MetadataObject):
    _collection_sets = []
    field = models.ForeignKey(Field)    
    value = models.CharField(max_length=1024, blank=True)
    description = models.TextField(blank=True)
    label = models.CharField(max_length=1024, blank=True)
    _container_field = "field_id"
    Field._collection_sets.append('validvalue_set')

class Property(Field):
    _collection_sets = []
    objecttype = models.ForeignKey(ObjectType)
    ucd = models.CharField(max_length=1024, blank=True)
    _container_field = "objecttype_id"
    ObjectType._collection_sets.append('property_set')

class PropertyGroup(MetadataObject):
    _collection_sets = []
    objecttype = models.ForeignKey(ObjectType)
    name = models.CharField(max_length=256)
    description = models.TextField(blank=True)
    properties = models.ManyToManyField(Property)
    _container_field = "objecttype_id"
    ObjectType._collection_sets.append('propertygroup_set')
